import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Trap here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Trap extends Held
{
    /**
     * Act - do whatever the Trap wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int GrungG = 1;
    boolean Wartestart;
    boolean WartestartHP;
    int WZ = 0; // Wartezeit
    int WZHP = 0; //wartezeit für lebenverlieren
    int spZahl = 0; // Sprungzahl
    boolean sprung = false;
    double Sprunghöhe = -10;
    int BW; // Bildwechsel
    int Lbenzw = Leben;
    Waffe Waf;
    double x=0; 
    ///////////////////////////////////7/////////////
    public Trap(){
        getImage().scale(80,250);
        Leben = 100; 
        Welt W1 = (Welt) getWorld();
    }

    public void act() 
    {
        //if(getWorld().pause())return;
        laufen();
        Waffenwechsel();
        Sprung();
        angriff();
        GegnerBeruehren();
        //Sterben();
        ((Welt)getWorld()).tripLebZeichnen(Leben);
    }  


    public void angriff(){
        if( Waf != null){
            if(Waf.AReich == 0){
                Waf = null;
            }
            return;
        }

        if(Greenfoot.isKeyDown("space")){
            Waf = new Waffe(Waffe,Rm[Waffe],Sm[Waffe]);
            Welt W1 = (Welt) getWorld();
            if(BR == -1){
                W1.addObject(Waf ,getX()-getImage().getWidth()/2,(int)(getY()-getImage().getHeight()*0.10));

            }
            else{
                W1.addObject(Waf ,getX()+getImage().getWidth()/2,(int)(getY()-getImage().getHeight()*0.10));

            }
            Waf.angriffsanimation(this);

        }

    }

    public void laufen(){
        double bla = GrungG*Geschwindigkeit;
        Welt W1 = (Welt) getWorld();
        if(Greenfoot.isKeyDown("A")){
            if(getX()>bla)move(-(int)bla);
            if(BR == 1)BW = 0;
            BR = -1;

            BW = BW +1;
            if(BW==1){
                setImage("agbot schritt 2s.png");

            }
            else if(BW == 15){
                setImage("agbots.png");  
            }
            else if(BW== 30){
                setImage("agbot schritt 1s.png");
            }
            else if(BW == 45){
                setImage("agbots.png");
            }
            else if(BW==60){
                setImage("agbot schritt 2s.png");
                BW = 0;
            }
        }
        if(Greenfoot.isKeyDown("D")){
            if (getX()<1024 - bla) move((int)bla);
            if(BR == -1)BW = 0;
            BR = 1;

            BW = BW +1;
            if(BW==1){
                setImage("agbot schritt 2.png");

            }
            else if(BW == 15){
                setImage("agbot.png");  
            }
            else if(BW== 30){
                setImage("agbot schritt 1.png");
            }
            else if(BW == 45){
                setImage("agbot.png");
            }
            else if(BW==60){
                setImage("agbot schritt 2.png");
                BW = 0;
            }
        }

    }

    public void Sprung(){
        if(x == 0 ){
            sprung = false;
        }
        if(sprung == true){

            setLocation ( getX(), getY()+(int)(2*x) );
            x = x+0.5;
            return;
        }
        if(Greenfoot.isKeyDown("W")){
            if(atBottom() == true || onGround() == true){
                sprung = true;

                x = Sprunghöhe;
            }
        }
        if(sprung == false&& onGround() == false && atBottom() == false){
            if(x == 3){
                setLocation ( getX(), getY()+(int)(2*x) );
                return;
            }
            setLocation ( getX(), getY()+(int)(2*x) );
            x = x+0.5;

        }
    }

    public void Sprung2(){

        if(x == 0 ){

            sprung = false;
        }
        if(sprung == true){

            setLocation ( getX(), getY()+(int)(1.5*x) );
            x = x+0.5;
            return;
        }

        sprung = true;
        x = Sprunghöhe;
        
        if(sprung == false&& onGround() == false && atBottom() == false){
            if(x == 3){
                setLocation ( getX(), getY()+(int)(2*x) );
                return;
            }
            setLocation ( getX(), getY()+(int)(2*x) );
            x = x+0.5;

        }
    }

    public boolean onGround()
    {
        Object under = getOneObjectAtOffset(0, (int)(getImage().getHeight()*0.5), null);
        return under != null;
    }

    private void GegnerBeruehren()
    {
        warteZeitHP();
        if(getOneIntersectingObject(Gegner.class)!=null)
        {
            if(WartestartHP == true)return;
            int i = getOneIntersectingObject(Gegner.class).getY();
            if((getY()+this.getImage().getHeight()*0.5)<i)
            {
                ((Gegner)getOneIntersectingObject(Gegner.class)).hit(20*ATK); Sprung2();

            }
            else
            {
                Leben=Leben -10;
                ((Welt)getWorld()).trapLebZeichnen(Leben);
                int count = 0;
                WartestartHP = true ;
            }
        }
        else return;
    }

    private boolean atBottom()
    {
        return getY() >= getWorld().getHeight()-getImage().getHeight()*0.5;
    }

    public void Waffenwechsel(){
        if(Wartestart == true){
            warteZeit();
            return;
        }
        if(Greenfoot.isKeyDown("Q")){
            Wartestart = true;
            if(Waffe == 5){
                Waffe = 1;
            }
            else{ Waffe = Waffe+1;
            }
        }

    }

    public void  warteZeit(){

        if(Wartestart == true){
            WZ = WZ +1;

        }
        if(WZ == 20){
            Wartestart = false;

            WZ = 0;
        }
    }

    public void  warteZeitHP(){

        if(WartestartHP == true){
            WZHP = WZHP +1;

        }
        if(WZHP == 25){
            WartestartHP = false;

            WZHP = 0;
        }
    }

    public void Sterben(){
        if(Leben <= 0){
            Welt W1 = (Welt) getWorld();
            W1.removeObject(this);

        }

        
    }

}
